package com.honyelchak.shortlink.admin.remote;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.honyelchak.shortlink.admin.remote.req.*;
import com.honyelchak.shortlink.admin.remote.resp.ShortLinkCountQueryRespDTO;
import com.honyelchak.shortlink.admin.remote.resp.ShortLinkCreateRespDTO;
import com.honyelchak.shortlink.admin.remote.resp.ShortLinkPageRespDTO;
import com.honyelchak.shortlink.base.common.convention.result.Result;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 短链接中台远程调用服务
 */
public interface ShortLinkRemoteService {

    default Result<IPage<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO reqDTO){
        Map<String, Object> requestMap = new HashMap<>();

        requestMap.put("gid", reqDTO.getGid());
        requestMap.put("current", reqDTO.getCurrent());
        requestMap.put("size", reqDTO.getSize());

        String resultPageStr = HttpUtil.get("http://localhost:8002/api/short-link/v1/page", requestMap);
        return JSON.parseObject(resultPageStr, new TypeReference<>(){});
    }

    default Result<ShortLinkCreateRespDTO> createShortLink(ShortLinkCreateReqDTO reqDTO){
        String resultStr = HttpUtil.post("http://localhost:8002/api/short-link/v1/create", JSON.toJSONString(reqDTO));
        return JSON.parseObject(resultStr, new TypeReference<>(){});
    }


    default Result<List<ShortLinkCountQueryRespDTO>> listGroupShortLinkCount(List<String> gids){
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("requestParam", gids);
        String resultStr = HttpUtil.get("http://localhost:8002/api/short-link/v1/count", paramMap);
        return JSON.parseObject(resultStr, new TypeReference<>(){});
    }

    default void updateShortLink(ShortLinkUpdateReqDTO requestParams) {
        HttpUtil.post("http://localhost:8002/api/short-link/v1/update", JSON.toJSONString(requestParams));
    }

    /**
     * 根据 URL 获取标题
     *
     * @param url 目标网站地址
     * @return 网站标题
     */
    default Result<String> getTitleByUrl(@RequestParam("url") String url) {
        String resultStr = HttpUtil.get("http://127.0.0.1:8002/api/short-link/v1/title?url=" + url);
        return JSON.parseObject(resultStr, new TypeReference<>() {
        });
    }

    default void saveRecycleBin(RecycleBinSaveReqDTO requestParam) {
        HttpUtil.post("http://localhost:8002/api/short-link/v1/recycle-bin/save", JSON.toJSONString(requestParam));
    }
    /**
     * 分页查询回收站短链接
     *
     * @param requestParam 分页短链接请求参数
     * @return 查询短链接响应
     */
    default Result<IPage<ShortLinkPageRespDTO>> pageRecycleBinShortLink(ShortLinkRecycleBinPageReqDTO requestParam) {
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("gidList", requestParam.getGidList());
        requestMap.put("current", requestParam.getCurrent());
        requestMap.put("size", requestParam.getSize());
        String resultPageStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/page", requestMap);
        return JSON.parseObject(resultPageStr, new TypeReference<>() {
        });
    }

    default void recoverRecycleBin(RecycleBinRecoverReqDTO requestParam) {

    }


}
